package com.baselibrary.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import com.baselibrary.BuildConfig;
import com.baselibrary.Constants;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:hanke 2022/7/4
 * desc:
 */
public class AppUtils {

    /**
     * 手机品牌
     */
    // 小米
    public static final String PHONE_XIAOMI = "xiaomi";
    // 华为
    public static final String PHONE_HUAWEI1 = "Huawei";
    // 华为
    public static final String PHONE_HUAWEI2 = "HUAWEI";
    // 华为
    public static final String PHONE_HUAWEI3 = "HONOR";
    // 魅族
    public static final String PHONE_MEIZU = "Meizu";
    // 索尼
    public static final String PHONE_SONY = "sony";
    // 三星
    public static final String PHONE_SAMSUNG = "samsung";
    // LG
    public static final String PHONE_LG = "lg";
    // HTC
    public static final String PHONE_HTC = "htc";
    // NOVA
    public static final String PHONE_NOVA = "nova";
    // OPPO
    public static final String PHONE_OPPO = "OPPO";
    // 乐视
    public static final String PHONE_LeMobile = "LeMobile";
    // 联想
    public static final String PHONE_LENOVO = "lenovo";

    // 检索筛选后返回
    public static List<String> hasMap(Context context) {
        List<String> mapsList = mapsList();
        List<String> backList = new ArrayList<>();
        for (int i = 0; i < mapsList.size(); i++) {
            boolean avilible = isAvilible(context, mapsList.get(i));
            if (avilible) {
                backList.add(mapsList.get(i));
            }
        }
        return backList;
    }

    // 检索地图软件
    public static boolean isAvilible(Context context, String packageName) {
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<>();
        //从pinfo中将包名字逐一取出，压入pName list中
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }

    public static List<String> mapsList() {
        boolean isHuaWeiPhone = isHuaWeiPhone();
        List<String> maps = new ArrayList<>();
        if (isHuaWeiPhone) {
            maps.add("com.huawei.appmarket");
            return maps;
        }
        maps.add("com.tencent.android.qqdownloader");
        maps.add("com.qihoo.appstore");
        maps.add("com.baidu.appsearch");
        maps.add("com.xiaomi.market");
        maps.add("com.wandoujia.phoenix2");
        maps.add("com.huawei.appmarket");
        maps.add("com.taobao.appcenter");
        maps.add("com.hiapk.marketpho");
        maps.add("cn.goapk.market");
        maps.add("com.meizu.mstore");
        maps.add("com.oppo.market");
        maps.add("com.bbk.appstore");
        maps.add("com.sec.android.app.samsungapps");
        return maps;
    }

    public static boolean isHuaWeiPhone() {
        String brand = Build.BRAND;
        return (!TextUtils.isEmpty(brand) && (PHONE_HUAWEI1.equalsIgnoreCase(brand) || PHONE_HUAWEI3.equals(brand)));
    }

    /**
     * 跳转到应用市场app详情界面
     *
     * @param appPkg    App的包名
     * @param marketPkg 应用市场包名
     */
    public static void launchAppDetail(Context context, String appPkg, String marketPkg) {
        try {
            if (TextUtils.isEmpty(appPkg))
                return;
            Uri uri = Uri.parse("market://details?id=" + appPkg);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            if (!TextUtils.isEmpty(marketPkg))
                intent.setPackage(marketPkg);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //分享域名
    public static String getSaveShareHostUrl() {
        String hostUrl = SpUtils.getString(ContextUtils.getCurApplication(), Constants.KEY_SHARE_URL);
        return hostUrl.equals("") ? BuildConfig.URL_SHENCE : hostUrl;
    }

    public static void setNavigationBarColor(Activity activity,Boolean isBlack) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            activity.getWindow().setNavigationBarColor(Color.parseColor(isBlack?"#000000":"#FFFFFF"));
    }

    public static String getVersionName() {
        try {
            return ContextUtils.getCurApplication().getPackageManager().getPackageInfo(ContextUtils.getCurApplication().getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException ex) {
            return "";
        }
    }

}
